package com.linkhub.oms.dao.mpservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.linkhub.oms.dao.mapper.ShippingLogisticsMappingEntityMapper;
import com.linkhub.oms.dao.mapper.ShopShippingEntityMapper;
import com.linkhub.oms.dao.mpservice.IChannelPriorityService;
import com.linkhub.oms.entity.*;
import com.linkhub.oms.entity.resulttype.ShopShippingChannelPriorityEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * @author Jaycle
 * @version 1.0.0
 * @Description ChannelPriorityServiceImpl.java
 * @createTime 2024/05/03 15:24
 */
@Slf4j
@Service
public class ChannelPriorityServiceImpl extends ServiceImpl<ShopShippingEntityMapper, ShopShippingEntity> implements IChannelPriorityService {

    @Resource
    private ShopShippingEntityMapper shopShippingEntityMapper;

    @Resource
    private ShippingLogisticsMappingEntityMapper shippingLogisticsMappingEntityMapper;

    /**
     * @param userID
     * @return
     */
    @Override
    public List<ShopShippingChannelPriorityEntity> queryShopByUserID(String userID) {
        MPJLambdaWrapper<ShopShippingEntity> lambdaWrapper = JoinWrappers.lambda(ShopShippingEntity.class);
        lambdaWrapper
                .select(ShopEntity::getUserId,
                        ShopEntity::getSysShopId,
                        ShopEntity::getShopName)
                .select(ShopShippingEntity::getUserId,
                        ShopShippingEntity::getSysShopId,
                        ShopShippingEntity::getShippingName)
                .select(ShippingLogisticsMappingEntity::getSysShopId,
                        ShippingLogisticsMappingEntity::getShippingName,
                        ShippingLogisticsMappingEntity::getChannelCode,
                        ShippingLogisticsMappingEntity::getRanked)
                .select(LogisticEntity::getChannelCode,
                        LogisticEntity::getChannelEName,
                        LogisticEntity::getChannelCName)
                .eq(ShopEntity::getUserId, userID)
                .rightJoin(ShopEntity.class,
                        on -> on.eq(ShopEntity::getSysShopId, ShopShippingEntity::getSysShopId)
                                .eq(ShopEntity::getUserId, ShopShippingEntity::getUserId))
                .leftJoin(ShippingLogisticsMappingEntity.class,
                        on -> on.eq(ShippingLogisticsMappingEntity::getSysShopId, ShopShippingEntity::getSysShopId)
                                .eq(ShippingLogisticsMappingEntity::getShippingName, ShopShippingEntity::getShippingName))
                .leftJoin(LogisticEntity.class,
                        on -> on.eq(LogisticEntity::getChannelCode, ShippingLogisticsMappingEntity::getChannelCode))
        ;
        List<ShopShippingChannelPriorityEntity> resultList = shopShippingEntityMapper.selectJoinList(ShopShippingChannelPriorityEntity.class, lambdaWrapper);
        return resultList;
    }

    /**
     * @param sysShopId
     * @param shippingName
     */
    @Override
    public void deleteChannelPriorityByShopIDAndShippingName(String sysShopId, String shippingName) {
        // 使用 LambdaQueryWrapper 构建删除条件
        LambdaQueryWrapper<ShippingLogisticsMappingEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShippingLogisticsMappingEntity::getSysShopId, sysShopId)
                .eq(ShippingLogisticsMappingEntity::getShippingName, shippingName);

        // 删除符合条件的记录
        shippingLogisticsMappingEntityMapper.delete(wrapper);
    }

    /**
     * 根据 sysShopId、shippingName、channelCode 和 priority 插入新的记录到 ShippingLogisticsMappingEntity 表
     *
     * @param sysShopId    系统商店ID
     * @param shippingName 运费名称
     * @param channelCode  渠道编码
     * @param priority     排序
     */
    @Override
    public void insertChannelPriorityByShopIDAndShippingName(String sysShopId, String shippingName, String channelCode, Integer priority) {
        // 创建一个新的 ShippingLogisticsMappingEntity 实体对象
        ShippingLogisticsMappingEntity entity = new ShippingLogisticsMappingEntity();
        entity.setSysShopId(sysShopId);
        entity.setShippingName(shippingName);
        entity.setChannelCode(channelCode);
        entity.setRanked(priority);

        // 调用 MyBatis-Plus 提供的插入方法插入到数据库中
        shippingLogisticsMappingEntityMapper.insert(entity);
    }

    @Override
    public void saveShippingName(String userId, String sysShopId, Set<String> shippingNames) {
        // 查询数据库中是否已存在指定商家ID和系统商店ID的记录
        LambdaQueryWrapper<ShopShippingEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShopShippingEntity::getUserId, userId)
                .eq(ShopShippingEntity::getSysShopId, sysShopId);
        List<ShopShippingEntity> existingShippingList = shopShippingEntityMapper.selectList(queryWrapper);

        // 遍历运费名称集合，对每个运费名称进行处理
        for (String shippingName : shippingNames) {
            // 判断当前运费名称是否已存在于数据库中的记录中
            boolean exists = existingShippingList.stream()
                    .anyMatch(entity -> entity.getShippingName().equals(shippingName));
            if (!exists) {
                // 如果不存在，则新增一条记录
                ShopShippingEntity newShipping = new ShopShippingEntity();
                newShipping.setUserId(userId);
                newShipping.setSysShopId(sysShopId);
                newShipping.setShippingName(shippingName);
                shopShippingEntityMapper.insert(newShipping);
            }
        }
    }
}
